<?php
namespace app\admin\controller\system;
use app\admin\model\system\NavMenuContentModel;
use app\admin\model\system\NavMenuModel;
use app\common\annotation\NodeAnnotation;
use app\common\controller\Admin;
use app\common\traits\ControllerCurd;
use think\Request;
/**
 * @package app\admin\controller\system
 * @NodeAnnotation(
 *     title="导航管理",
 *     is_menu=0,
 *     is_view=0
 * )
 */
class Nav extends Admin
{
    use ControllerCurd;
    public $model;

    public function initialize()
    {
        parent::initialize();
        $this->model= new \app\admin\model\system\NavMenuModel();
    }



    /**
     * @NodeAnnotation(
     *     title="导航管理",
     *     parent="admin/system.nav",
     *     MenuRoot="admin/system.setting",
     *     is_menu=1,
     * )
     */
    public function index(Request $request)
    {
        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['name',"like","%$keywords%"]);

        $this->assign([
            "navList" => $this->model->where($where)->paginate(),
            "search"    => [
                "keywords" => $keywords
            ]
        ]);
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加导航",
     *     parent="admin/system.nav",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function addnav(Request $request)
    {
        $input = $request->post();
        if(!isset($input['is_main'])) $input['is_main'] = 0;
        $this->add(url('admin/system.nav/index'),function($model,$data){
            if(isset($data['is_main']) && $data['is_main']==1){
                $model->where("id","not in",$data['id'])
                    ->where("is_main",1)
                    ->update([
                        "is_main" => 0
                    ]);
            }
        },$input);
        return $this->fetch();
    }


    /**
     * @NodeAnnotation(
     *     title="编辑导航",
     *     parent="admin/system.nav",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function editnav(Request $request,$id=0)
    {
        $input = $request->post();
        if(!isset($input['is_main'])) $input['is_main'] = 0;
        $this->edit(url('admin/system.nav/index'),function($model,$data){
            if(isset($data['is_main']) && $data['is_main']==1){
                $model->where("is_main","not in",$data['id'])
                    ->where("is_main",1)
                    ->update([
                        "is_main" => 0
                    ]);
            }
        },$input);

        $nav = $this->model->findData($id);

        $this->assign([
            "nav"   => $nav,
        ]);

        return $this->fetch();
    }


    /**
     * @NodeAnnotation(
     *     title="删除导航",
     *     parent="admin/system.ad",
     *     MenuRoot="admin/system.setting",
     *     is_view=0,
     *     is_menu=0
     * )
     */
    public function delnav(Request $request)
    {
        $this->del();
    }



    /**
     * @NodeAnnotation(
     *     title="导航菜单",
     *     parent="admin/system.nav",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function navmenus(Request $request,$nav_id=0)
    {
        $this->model = new \app\admin\model\system\Nav();

        $keywords  = $request->param("keywords");

        $where = [];

        if($keywords) array_push($where,['title',"like","%$keywords%"]);
        if($nav_id) array_push($where,['nav_id',"eq",$nav_id]);

        $this->assign([
            "navList"   => $this->model->where($where)->paginate(),
            "search"    => [
                "keywords" => $keywords
            ]
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="添加导航菜单",
     *     parent="admin/system.nav",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function addnavmenus(Request $request)
    {
        $this->model = new \app\admin\model\system\Nav();

        if($request->isAjax())
        {
            $menu = $request->param('menuType');
            if($menu=='tree')
            {
                $TreeMenu = $this->model->select()->toArray();
                array_push($TreeMenu,['id'=>0,"title"=>"顶级导航"]);
                return json($TreeMenu);
            }
        }
        $this->add(url('admin/system.nav/navmenus',['nav_id'=>request()->param('nav_id')]),function($model,$data){
            if(!empty($data['id'])){
                NavMenuContentModel::create([
                    "content" => isset($data['content'])?$data['content']:'',
                    "nav_id"  => $data['id']
                ]);
            }
        });
        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="编辑导航菜单",
     *     parent="admin/system.nav",
     *     MenuRoot="admin/system.setting",
     *     is_menu=0
     * )
     */
    public function editnavmenus(Request $request,$id=0)
    {
        $this->model = new \app\admin\model\system\Nav();

        if($request->isAjax())
        {
            $menu = $request->param('menuType');
            if($menu=='tree')
            {
                $TreeMenu = $this->model->select()->toArray();
                array_push($TreeMenu,['id'=>0,"title"=>"顶级导航"]);
                return json($TreeMenu);
            }
        }
        $this->edit(url('admin/system.nav/navmenus',['nav_id'=>request()->param('nav_id')]),function($model,$data){
            if(!empty($data['id'])){
                $content = NavMenuContentModel::where("nav_id",$data['id'])->find();
                if($content){
                    NavMenuContentModel::where("nav_id",$data['id'])->update([
                        "content" => isset($data['content'])?$data['content']:'',
                    ]);
                }else{
                    NavMenuContentModel::create([
                        "content" => isset($data['content'])?$data['content']:'',
                        "nav_id"  => $data['id']
                    ]);
                }
            }
        });

        $nav = $this->model->findData($id);
        $nav['content'] = NavMenuContentModel::where("nav_id",$id)->find()['content'];

        $level = $this->model->findData($nav['pid']);

        $this->assign([
            "nav"   => $nav,
            "level" => $level
        ]);

        return $this->fetch();
    }

    /**
     * @NodeAnnotation(
     *     title="删除导航菜单",
     *     parent="admin/system.ad",
     *     MenuRoot="admin/system.setting",
     *     is_view=0,
     *     is_menu=0
     * )
     */
    public function delnavmenus(Request $request)
    {
        $this->model = new \app\admin\model\system\Nav();
        $this->del(url('admin/system.nav/navmenus',['nav_id'=>request()->param('nav_id')]),function($model,$data){
            NavMenuContentModel::where("nav_id","in",$data)->delete();
        });
    }

}